Providing a Flex Front End to Your Struts Applications
Table of Contents
Approach 2: SOAP Integration
The traditional HTTP request/response integration approach works well because it fits perfectly in Struts' workflow and, at the same time, provides the end-user with all the benefits of Rich Internet Applications.
So why a second integration approach? The fact is that with the advent of new technologies such as Rich Internet Applications and SOAP, some of the problems that the Struts framework solved in a traditional HTML / HTTP environment have disappeared. For example, Rich Internet Applications allow you to maintain state information at the client side, and SOAP allows you to invoke methods on remote objects using the HTTP protocol.
The integration approach in this section does the following:
- Fully leverages the capabilities of Rich Internet Applications and SOAP.
- Bypasses Struts features that you no longer need when you use these technologies.
- Allows your client application to interact directly with the relevant Struts components exposed as services.
Using a Service Façade
Because, in the Struts framework, controller components rely heavily on the HTTP request/response mechanism, the Request and Response objects are omnipresent in their API. To present an API that doesn't have these dependencies, you can add a service façade in front of the Struts objects you want to expose. The service façade is a frequently used pattern in a Service-Oriented Architecture (SOA). It makes your existing Struts model available to other service clients outside of Flex.
The following diagram illustrates the workflow of the Registration application implemented using this approach (RegistrationService is the service façade). Notice that the client now communicates with the server on a component-to-service basis instead of on a page-to-front-controller basis.
Figure 4. The workflow of the Registration application using the SOAP approach.
Note: Struts is a flexible framework that developers use in many different ways. The specifics of the solution presented in this section might therefore not be directly applicable to your application, but the overall approach is. The point of this approach is that your client application can now connect directly to the relevant parts of your Struts applications, whatever these relevant parts are in the context of your application.
The RegistrationService class is defined as follows:
package samples.struts;
import org.apache.struts.action.*;
import java.util.*;
public class RegistrationService {
public ArrayList register(RegistrationForm form) {
// Validate form fields
ActionErrors errors = form.validate(null, null);
// Execute Action only if form validation succeeded
if (errors.isEmpty()) {
RegistrationAction action=new RegistrationAction();
errors = action.execute(form);
}
// Build an array of error messages to be returned to client application
ArrayList messages = new ArrayList();
Iterator i = errors.get();
while (i.hasNext()) {
messages.add(((ActionError) i.next()).getKey());
}
return messages;
}
}
Code Highlights
Notice that the register() method takes a RegistrationForm
object as a parameter: In this case, you use the Struts-based RegistrationForm
JavaBean as a value object to transfer the registration data from the
client to the server. The workflow of this interaction is as follows:
- The Flex application has an ActionScript version of that object. The application populates an instance of that object.
- The ActionScript object is serialized into the SOAP request at the client side.
- The object is deserialized into an instance of the RegistrationForm JavaBean at the server side.
You also refactor the RegistrationAction class to support both request/response and SOAP invocations without duplicating code.
package samples.struts;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public final class RegistrationAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
ActionErrors errors = execute(form);
if (!errors.isEmpty()) {
saveErrors(request,errors);
return new ActionForward(mapping.getInput());
}
request.setAttribute("registration", form);
return mapping.findForward("success");
}
public ActionErrors execute(ActionForm form) {
ActionErrors errors = new ActionErrors();
RegistrationForm registrationForm = (RegistrationForm) form;
String email = registrationForm.getEmail();
// Dummy business logic: here is where you would typically communicate with a business object
if (email == null || email.indexOf("@") < 0) {
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.registration.failed"));
}
return errors;
}
}